//
//  XGMineItemTableViewCell.swift
//  TodayNews
//
//  Created by monkey on 2020/2/3.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit
import SnapKit
import Kingfisher
import SwiftTheme

class XGMineItemTableViewCell: UITableViewCell,XGRegisterReusableViewProtocol
{
    /// 设置项数据模型
    var itemModel: XGMineItemModel? {
        didSet {
            titleLabel.text = itemModel?.text
            subTitleLabel.text = itemModel?.grey_text
            var imageUrl:String?
            switch TodayNewsThemeManager.shared.currentTheme {
            case .day:
                imageUrl = itemModel?.icons?.day?.url_list?.first?.url
            case .night:
                imageUrl = itemModel?.icons?.night?.url_list?.first?.url
            }
            
            if imageUrl != nil {
                iconImageView.kf.setImage(with: URL(string: imageUrl!))
            }
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?)
    {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setUpUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
     
    /// 图标
    private lazy var iconImageView: UIImageView = UIImageView()
    /// 标题
    private lazy var titleLabel: UILabel = UILabel(text: "测试", textColor: UIColor.darkGray, fontSize: 17, textAlignment: .left)
    /// 副标题
    private lazy var subTitleLabel: UILabel = UILabel(text: "测试", textColor: UIColor.lightGray, fontSize: 15, textAlignment: .left)
    /// 箭头
    private lazy var arrowImageView: UIImageView = UIImageView(image: UIImage(named: "setting_rightarrow_8x14_"))
}

private extension XGMineItemTableViewCell
{
    /// 设置界面
    func setUpUI() -> Void
    {
        theme_backgroundColor = "colors.cell_background"
        titleLabel.theme_textColor = "colors.cell_title"
        
        contentView.addSubview(iconImageView)
        contentView.addSubview(titleLabel)
        contentView.addSubview(arrowImageView)
        contentView.addSubview(subTitleLabel)
        
        iconImageView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(20)
            make.centerY.equalToSuperview()
            make.size.equalTo(CGSize(width: 24, height: 24))
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(iconImageView.snp.right).offset(10)
            make.centerY.equalToSuperview()
        }
        
        arrowImageView.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-20)
            make.centerY.equalToSuperview()
        }
        
        subTitleLabel.snp.makeConstraints { (make) in
            make.right.equalTo(arrowImageView.snp.left).offset(-10)
            make.centerY.equalToSuperview()
            make.left.greaterThanOrEqualTo(titleLabel.snp.right).offset(10)
        }        
    }
}
